import { registerAs } from '@nestjs/config';

export interface mongoConfigInterface {
  host: string;
  port: string;
  db: string;
  username: string;
  password: string;
  authSource: string;
  uri: string;
}

export const mongoConfig: mongoConfigInterface = {
  host: process.env.mongo_host,
  port: process.env.mongo_port,
  db: process.env.mongo_db,
  username: process.env.mongo_username,
  password: process.env.mongo_password,
  authSource: 'admin',
  get uri() {
    if (mongoConfig.username && mongoConfig.password) {
      return `mongodb://${mongoConfig.username}:${mongoConfig.password}@${mongoConfig.host}:${mongoConfig.port}/${mongoConfig.db}?authSource=${mongoConfig.authSource}`;
    }
    return `mongodb://${mongoConfig.host}:${mongoConfig.port}/${mongoConfig.db}`;
  },
};

export const MongoConfig = registerAs('mongo', () => mongoConfig);
